package org.example.controller;

import org.example.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    private TestService testService;

    @GetMapping("getDatasByPage")
    public Map<String, Object> getDatasByPage(
            @RequestParam(required = false, defaultValue = "1") int start,
            @RequestParam(required = false, defaultValue = "20") int limit){
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("count", testService.countDatas());
        data.put("datas", testService.getDatas(start, limit));
        return data;
    }

    @GetMapping("deleteDatas")
    public Map<String, Object> deleteDatas(
            @RequestParam(required = false, defaultValue = "1") int start,
            @RequestParam(required = false, defaultValue = "20") int limit){
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("count", testService.deleteDatas());
        return data;
    }

    @GetMapping("addDatas")
    public Map<String, Object> addDatas(
            @RequestParam(required = false, defaultValue = "15") int limit){
        List<Map<String, Object>> datas = new ArrayList<>(limit);
        for (int i = 0; i < limit; i++) {
            Map<String, Object> data = new HashMap<>(4);
            data.put("id", i+1);
            data.put("age", i+1);
            data.put("city", "xxx");
            data.put("money", 5.5);
            //id, age, city, money
            datas.add(data);
        }
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("count", testService.addDatas(datas));
        return data;
    }
}
